package com.example.guiguiapplicatio;

import android.app.AlertDialog;
import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;

import com.example.guiguiapplicatio.adapter.LocalSaveActivityAdapter;
import com.example.guiguiapplicatio.utils.PreferencesUtil;
import com.google.android.material.snackbar.Snackbar;

import androidx.appcompat.app.AppCompatActivity;

import android.util.Log;
import android.view.View;

import androidx.core.view.WindowCompat;
import androidx.navigation.NavController;
import androidx.navigation.Navigation;
import androidx.navigation.ui.AppBarConfiguration;
import androidx.navigation.ui.NavigationUI;
import androidx.recyclerview.widget.LinearLayoutManager;

import com.example.guiguiapplicatio.databinding.ActivityLocalSaveBinding;

import java.util.ArrayList;
import java.util.List;

public class LocalSaveActivity extends AppCompatActivity {

    private LocalSaveActivityAdapter adapter;
    private ActivityLocalSaveBinding binding;
    private List<String> itemDescriptionList = new ArrayList<>();
    private String dataType = "";
    private String data = "";
    private boolean IS_SAVE = false;
    private SharedPreferences sp;
    private SharedPreferences.Editor edit;
    private int DATA_SIZE = 6;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        binding = ActivityLocalSaveBinding.inflate(getLayoutInflater());
        setContentView(binding.getRoot());

        acceptParameters();
        initData();

        adapter = new LocalSaveActivityAdapter(itemDescriptionList, dataType, this, edit,sp, DATA_SIZE,data,IS_SAVE);
        binding.recyLayout.setLayoutManager(new LinearLayoutManager(this));
        binding.recyLayout.setAdapter(adapter);
        setDoubtBtn();
        setBackBtn();

    }

    private void setDoubtBtn() {
        binding.doubt.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                AlertDialog aldg;
                AlertDialog.Builder adBd = new AlertDialog.Builder(v.getContext());
                adBd.setTitle("解惑");
                adBd.setMessage("如果您在随访界面输入了数据，并点击本地保存按钮进来，您可以选择点击+号把随访界面的数据作为一条记录添加到本地数据中，也可点击已有的数据进行覆盖。" +
                        "若您是点击本地取出按钮进来，您可以选择一个记录点击确定后即可载入到随访界面。本页面的功能主旨是为了能在非联网状态下依旧可以进行随访并保存直到联网后将数据导出并上传。");
                adBd.setNegativeButton("取消",null);
                aldg = adBd.create();
                aldg.show();
            }
        });
    }

    private void setBackBtn() {
        binding.back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
    }

    private void initData() {
        for (int i = 0; i < DATA_SIZE; i++) {
            if (sp.getString(dataType+"_DSC_" + i, null) != null) {

                itemDescriptionList.add(sp.getString(dataType+"_DSC_" + i, null));

            }
        }
    }

    private void acceptParameters() {
        sp = getSharedPreferences("local_save_sp_info", Context.MODE_PRIVATE);
        edit = sp.edit();
        IS_SAVE = getIntent().getBooleanExtra("IS_SAVE", false);
        dataType = getIntent().getStringExtra("dataType");
        if (IS_SAVE){
            data = getIntent().getStringExtra("data");
        }


    }

}